package com.miplus.ccrm.core.controller;

import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JSysUser;
import com.miplus.ccrm.common.PageData;
import com.miplus.ccrm.common.R;
import com.miplus.ccrm.core.controller.vo.LabelResVo;
import com.miplus.ccrm.core.controller.vo.cust.*;
import com.miplus.ccrm.core.security.JCommonData;
import com.miplus.ccrm.core.service.cust.*;
import com.miplus.ccrm.core.utils.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.jetbrains.annotations.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 2-2.客户及工单服务接口
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/cust")
public class CustController {
    @Resource
    private ConCustomerService conCustomerService;
    @Resource
    private ConContactService conContactService;
    @Resource
    private ConAddressService conAddressService;
    @Resource
    private ConCasesService conCasesService;
    @Resource
    private ConCaseprocessService conCaseprocessService;
    @Resource
    private JCommonData jCommonData;

    @Resource
    private FiCustomerScorecardHistService customerScorecardHistService;

    /**
     * 取客户信息LIST
     */
    @PostMapping("/customerQuery")
    public R<PageData<CustResVo>> customerQuery(@RequestBody @Validated CustReqVo custReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.SuccessPage(conCustomerService.customerQuery(user,custReqVo));
    }
    /**
     * 取客户信息LIST 金额字段统计
     */
    @PostMapping("/customerQueryStatistics")
    public R<CustStatisticsResVo> customerQueryStatistics(@RequestBody @Validated CustReqVo custReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(conCustomerService.customerQueryStatistics(user,custReqVo));
    }

    /**
     * 取客户信息One
     */
    @GetMapping("/customerInfo")
    public R<CustResVo> customerInfo(@RequestParam @NotNull String customernumber){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(conCustomerService.customerInfo(user,customernumber));
    }

    /**
     * 更新客户标签
     */
    @PostMapping("/updateCustTags")
    public R<CmdResVo> updateTags(@RequestBody @Validated CustTagReqVo custTagReqVo){
        return R.Success(conCustomerService.updateTags(custTagReqVo));
    }

    /**
     * 取客户对应联系人LIST
     */
    @GetMapping("/contactList")
    public R<PageData<ContResVo>> contactList(@Validated ContReqVo contReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.SuccessPage(conContactService.contactList(user,contReqVo));
    }

    /**
     * 取客户本人电话下拉列表
     */
    @GetMapping("/contactSimplifyList")
    public R<List<ConSimplifyResVo>> contactSimplifyList(@Validated @Param("customernumber") String customernumber){

        return R.Success(conContactService.contactSimplifyList(customernumber));
    }

    /**
     * 取客户对应联系人One
     */
    @GetMapping("/contactInfo")
    public R<ContResVo> contactInfo(@RequestParam @NotNull Long contactnumber){
        return R.Success(conContactService.contactInfo(contactnumber));
    }

    /**
     * 添加客户联系人
     */
    @PostMapping("/contactInsert")
    public R<CmdResVo> contactInsert(@RequestBody @Validated ContEditVo contEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(conContactService.insertContact(usrid,contEditVo));
    }

    /**
     * 修改客户联系人
     */
    @PostMapping("/contactUpdate")
    public R<CmdResVo> updateContact(@RequestBody @Validated ContEditVo contEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(conContactService.updateContact(usrid,contEditVo));
    }

    /**
     * 删除客户联系人
     */
    @PostMapping("/contactDelete")
    public R<CmdResVo> contactDelete(@RequestBody @Validated ContDelVo contDelVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(conContactService.deleteContact(usrid,contDelVo.getContactnumber()));
    }

    /**
     * 取客户对应地址LIST
     */
    @GetMapping("/addressList")
    public R<PageData<AddrResVo>> addressList(@Validated AddrReqVo addrReqVo){
        JSysUser user = jCommonData.getCurrUsr();
        return R.SuccessPage(conAddressService.addressList(user, addrReqVo));
    }

    /**
     * 取客户对应地址One
     */
    @GetMapping("/addressInfo")
    public R<AddrResVo> addressInfo(@RequestParam @NotNull int addressnumber){
        JSysUser user = jCommonData.getCurrUsr();
        return R.Success(conAddressService.getAddressInfo(user, addressnumber));
    }

    /**
     * 添加客户地址
     */
    @PostMapping("/addressInsert")
    public R<CmdResVo> addressInsert(@RequestBody @Validated AddrEditVo addrEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(conAddressService.insertAddress(usrid,addrEditVo));
    }

    /**
     * 修改客户地址
     */
    @PostMapping("/addressUpdate")
    public R<CmdResVo> addressUpdate(@RequestBody @Validated AddrEditVo addrEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(conAddressService.updateAddress(usrid,addrEditVo));
    }

    /**
     * 删除客户地址
     */
    @PostMapping("/addressDelete")
    public R<CmdResVo> addressDelete(@RequestBody @Validated AddrDelVo addrDelVo) {
        return R.Success(conAddressService.deleteAddress(addrDelVo.getAddressnumber()));
    }

    /**
     * 取工单LIST
     */
    @PostMapping("/caseList")
    public R<PageData<CaseResVo>> caseList(@RequestBody @Validated CaseReqVo caseReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.SuccessPage(conCasesService.caseList(user,caseReqVo));
    }
    /**
     * 导出工单LIST
     */
    @PostMapping("/exportCaseList")
    public R<CmdResVo> exportCaseList(@RequestBody @Validated CaseReqVo caseReqVo, HttpServletResponse response){
        String $userid= jCommonData.getCurrUsrID();
        JSysUser user=jCommonData.getCurrUsr();
        return R.Success(conCasesService.exportCaseList($userid,user,caseReqVo,response));
    }

    /**
     * 取工单One
     */
    @GetMapping("/caseInfo")
    public R<CaseResVo> caseInfo(@RequestParam @NotNull Integer casenumber){
        return R.Success(conCasesService.caseInfo(casenumber));
    }

    /**
     * 添加工单
     */
    @PostMapping("/caseInsert")
    public R<CmdResVo> caseInsert(@RequestBody @Validated CaseAddVo caseAddVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(conCasesService.caseInsert(usrid,caseAddVo));
    }

    /**
     * 取工单进程LIST
     */
    @GetMapping("/caseProcessList")
    public R<List<CaseProcResVo>> caseProcessList(@RequestParam @NotNull Integer casenumber){
        return R.Success(conCaseprocessService.caseProcessList(casenumber));
    }

    /**
     * 添加工单进程
     */
    @PostMapping("/caseProcessAdd")
    public R<CmdResVo> caseProcessAdd(@RequestBody @Validated CaseProcAddVo caseProcAddVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(conCasesService.caseProcessAdd(usrid,caseProcAddVo));
    }

    /**
     * 添加工单进程 批量
     */
    @PostMapping("/caseProcessAddBatch")
    public R<CmdResVo> caseProcessAddBatch(@RequestBody @Validated CaseProcAddVo caseProcAddVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(conCasesService.caseProcessAddBatch(usrid,caseProcAddVo));
    }

    /**
     * 取工单处理部门（责任部门）LIST
     */
    @GetMapping("/getXbDistGrpList")
    public R<List<LabelResVo>> getXbDistGrpList(){
        return R.Success(conCasesService.getXbDistGrpList());
    }

    /**
     * 取安全电话号码
     */
    @GetMapping("/getSecuPhone")
    public R<CmdResVo> getSecuPhone(@RequestParam @NotNull Long contactnumber){
        return R.Success(conContactService.getSecuPhone(contactnumber));
    }


    /**
     * 客户评分历史详情
     * @param reqVo
     * @return
     */
    @PostMapping("/custSreHistPage")
    public R<PageData<CustSreHistPageResVo>> customerScorecardHistPage(@RequestBody @Validated CustSreHistPageReqVo reqVo){
//        JSysUser user= jCommonData.getCurrUsr();
        return R.SuccessPage(customerScorecardHistService.customerScorecardHistPage(reqVo));
    }

    /**
     * 同步C卡评分结果
     * @param reqVo
     * @return
     */
    @PostMapping("/synCustScore")
    public R<CmdResVo>synCustScore(@RequestBody @Validated CustScoreSynReqVo reqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(customerScorecardHistService.synCustScore(user,reqVo));
    }
}
